﻿package com.guavestudios.fcon.plugins {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	
	/**
	* ...
	* @author Default
	*/
	public class SimpleEditorSprite extends Sprite{
		
		protected var _field:TextField;
		protected var _text:String;
		
		protected var btnClose:Sprite;
		protected var btnResize:Sprite;
		
		protected var _width:Number=100;
		protected var _height:Number = 50;
		
		protected var clickX:Number;
		protected var clickY:Number;
		
		public function SimpleEditorSprite() {
			
			_field = new TextField();
			_field.border = true;
			_field.multiline = true;
			_field.type = TextFieldType.INPUT;
			
			btnClose = new Sprite();
			btnClose.graphics.beginFill(0xFF00000);
			btnClose.graphics.drawRect(0,0,20, 20);
			btnClose.graphics.endFill();
			addChild(btnClose);
			
			btnResize = new Sprite();
			btnResize.graphics.beginFill(0x666666);
			btnResize.graphics.drawRect(0,0,20, 20);
			btnResize.graphics.endFill();
			addChild(btnResize);
			
			addChild(_field);
			
			btnClose.addEventListener(MouseEvent.CLICK,btnClose_click);
			btnResize.addEventListener(MouseEvent.MOUSE_DOWN, btnResize_mousedown);
			this.addEventListener(MouseEvent.MOUSE_DOWN, this_mousedown);
			
			draw();
		}
		
		public function get text():String { return _field.text; }
		
		public function set text(value:String):void {
			_field.text = value;
		}
		
		public function setSize(w:Number, h:Number):void
		{
			_width = w;
			_height = h;
			draw();
		}
		public function setPosition(x:Number, y:Number):void
		{
			this.x = x;
			this.y = y;
		}
		public function draw():void
		{
			graphics.clear();
			graphics.beginFill(0xAAAAAA);
			graphics.drawRect(0, 0, _width, _height);
			graphics.endFill();
			
			_field.width = _width;
			_field.height = _height - btnClose.height;
			
			btnResize.x = _width - btnResize.width;
			btnResize.y = _height - btnResize.height;
			btnClose.x = _width - btnResize.width - btnClose.width;
			btnClose.y = _height - btnClose.height;
		}
		private function btnClose_click(e:MouseEvent):void {
			
			dispatchEvent(new Event(Event.SELECT));
		}
		private function btnResize_mousedown(e:MouseEvent):void {
			clickX = this.x;
			clickY = this.y;
			
			stage.addEventListener(MouseEvent.MOUSE_UP, btnResize_mouseup);
			e.stopPropagation();
		}
		
		private function btnResize_mouseup(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, btnResize_mouseup);
			this.setSize(
				Math.max(40, stage.mouseX-clickX),
				Math.max(40, stage.mouseY-clickY)
			);
		}
		
		
		private function this_mousedown(e:MouseEvent):void {
			clickX = this.mouseX;
			clickY = this.mouseY;
			
			stage.addEventListener(MouseEvent.MOUSE_UP, this_mouseup);
			
		}
		
		private function this_mouseup(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, this_mouseup);
			
			this.x = stage.mouseX-clickX;
			this.y = stage.mouseY-clickY;
		}
	}
	
}